home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / ACE / utils / Indent / indent.b
Text File  |  1994-10-22  |  5KB  |  222 lines

  1. ' ACE Basic Indenter
  2. ' Version 1.0 by Roland Acton - August 1994
  3. ' Internet address: xracton@ccvax.fullerton.edu
  4.  
  5. ' This program is public domain. You may use any part of it you
  6. ' wish in a program of your own. Please credit me if you do so.
  7.  
  8. declare sub shortint ParseCommandLine
  9.  
  10. declare function AllocMem& library exec
  11. declare function FreeMem& library exec
  12. declare function xOpen& library dos
  13. declare function xClose& library dos
  14. declare function xRead& library dos
  15. declare function xWrite& library dos
  16.  
  17. ' Change this constant if you want more (or less) indentation.
  18.  
  19. const indentamount=2
  20.  
  21. longint srcfilehandle,destfilehandle
  22.  
  23. shortint indent,bufferpos,buffercount
  24. longint bufferstart,fileline
  25. shortint linepos
  26. longint spacebuffer
  27. shortint z
  28. longint counter,indentskip
  29. shortint keywordcount
  30. longint keywordpointer
  31. shortint foundkeyword,x
  32. longint linepointer
  33. shortint pclsuccess,doweindent
  34. string a$,keyword$
  35. longint longcoerce
  36.  
  37. library exec
  38. library dos
  39. print "Indenter for ACE Basic"
  40. print "Version 1.0 by Roland Acton - August 1994"
  41. print
  42. pclsuccess=ParseCommandLine()
  43. if pclsuccess=1% then
  44.   bufferstart=AllocMem(256&,0&)
  45.   fileline=AllocMem(256&,0&)
  46.   spacebuffer=AllocMem(256&,0&)
  47.  
  48. ' Strictly speaking, I should check to see if the memory allocation
  49. ' fails, but if the system is that low on memory it's going to fail
  50. ' for other reasons in short order.
  51.  
  52.   for counter=spacebuffer to spacebuffer+252& step 4
  53.     pokel counter,&H20202020
  54.   next
  55.   buffercount=xRead(srcfilehandle,bufferstart,256&)
  56.   bufferpos=0%
  57.   indent=0%
  58.  
  59.   data "ASSEM","CASE","FOR","IF","ELSEIF","REPEAT","STRUCT"
  60.   data "SUB","WHILE"
  61.   data "ELSE"
  62.   data "ENDASSEM","ENDCASE","NEXT","ENDIF","UNTIL","ENDSTRUCT"
  63.   data "ENDSUB","WEND"
  64.   const maxkeywords=18,postrange=10,prerange=10
  65.  
  66.   longcoerce=varptr(keyword$)
  67.   x=0%
  68.   repeat
  69.     read a$
  70.     for z=1% to len(a$)
  71.       poke longcoerce,asc(mid$(a$,z,1))
  72.       ++longcoerce
  73.     next
  74.     poke longcoerce,0%
  75.     ++longcoerce
  76.     ++x
  77.   until x=maxkeywords
  78.  
  79. ' The section of code below is an internal buffering system. It
  80. ' pulls in 256 bytes at a time from the source file and extracts
  81. ' lines of source code from them.
  82.  
  83.   mainloop:
  84.   linepos=0%
  85.   repeat
  86.     z=peek(bufferstart+bufferpos)
  87.     poke fileline+linepos,z
  88.     ++bufferpos
  89.     ++linepos
  90.     if bufferpos=buffercount then
  91.       buffercount=xRead(srcfilehandle,bufferstart,256&)
  92.       bufferpos=0%
  93.     end if
  94.   until z=10% or buffercount=0% or linepos=256%
  95.  
  96. ' It's assumed that no line will be larger than 256 bytes. The extra
  97. ' condition above is just to stop the program from crashing if it's
  98. ' accidentally run on a binary file.
  99.  
  100.   for indentskip=fileline to fileline+linepos-1%
  101.     z=peek(indentskip)
  102.     if z<>32% then
  103.       exit for
  104.     end if
  105.   next
  106.   linepos=fileline+linepos-indentskip
  107.  
  108. ' The section of code above is designed to skip any indentation that
  109. ' already exists.
  110.  
  111. ' Now we have to determine which keyword, if any, the line begins
  112. ' with. We skip over spaces and convert alphabetic characters to
  113. ' uppercase.
  114.  
  115.   keywordcount=1%
  116.   keywordpointer=varptr(keyword$)
  117.   repeat
  118.     linepointer=indentskip
  119.     foundkeyword=1%
  120.     repeat
  121.       z=peek(linepointer)
  122.       while z=32%
  123.         ++linepointer
  124.         z=peek(linepointer)
  125.       wend
  126.       if z>=97% and z<=122% then
  127.         z=z-32%
  128.       end if
  129.       x=peek(keywordpointer)
  130.       if z<>x and x<>0% then
  131.         foundkeyword=0%
  132.       end if
  133.       ++linepointer
  134.       ++keywordpointer
  135.     until x=0%
  136.     if foundkeyword=1% then
  137.       goto keybreak
  138.     end if
  139.     ++keywordcount
  140.   until keywordcount>maxkeywords
  141.  
  142. ' If the line started with a valid keyword, its number is in
  143. ' keywordcount.
  144.  
  145.   keybreak:
  146.  
  147. ' Check for one of the pre-output indent changers.
  148.  
  149.   if keywordcount>=prerange and keywordcount<=maxkeywords then
  150.     indent=indent-indentamount
  151.     if indent<0% then
  152.       indent=0%
  153.     end if
  154.   end if
  155.  
  156. ' If this is a comment line, or is blank, we do not indent it at
  157. ' all.
  158.  
  159.   a$=chr$(peek(indentskip))+chr$(peek(indentskip+1))+chr$(peek(indentskip+2))
  160.   if left$(a$,1%)="'" or ucase$(a$)="REM" then
  161.     doweindent=0%
  162.   else
  163.     doweindent=1%
  164.   end if
  165.  
  166. ' Output the line.
  167.  
  168.   if linepos>1% and doweindent=1% then
  169.     longcoerce=indent
  170.     xWrite(destfilehandle,spacebuffer,longcoerce)
  171.   end if
  172.   longcoerce=linepos
  173.   xWrite(destfilehandle,indentskip,longcoerce)
  174.  
  175. ' Check for one of the post-output indent changers.
  176.  
  177.   if keywordcount<=postrange then
  178.     indent=indent+indentamount
  179.   end if
  180.  
  181. ' If there's still something left in the input buffer, we go back to
  182. ' get the next line.
  183.  
  184.   if buffercount>0% then
  185.     goto mainloop
  186.   end if
  187.   FreeMem(bufferstart,256&)
  188.   FreeMem(fileline,256&)
  189.   FreeMem(spacebuffer,256&)
  190.   xClose(srcfilehandle)
  191.   xClose(destfilehandle)
  192. end if
  193. library close exec
  194. library close dos
  195.  
  196.  
  197.  
  198. sub shortint ParseCommandLine
  199.   shared srcfilehandle,destfilehandle
  200.  
  201.   if argcount<>2 then
  202.     print "Error in command line"
  203.     print "Syntax: IndentACE <source-file> <destination-file>"
  204.     parsecommandline=0%
  205.   else
  206.     srcfilehandle=xOpen(arg$(1),1005&)
  207.     if srcfilehandle>0& then
  208.       destfilehandle=xOpen(arg$(2),1006&)
  209.       if destfilehandle>0& then
  210.         parsecommandline=1%
  211.       else
  212.         xClose(srcfilehandle)
  213.         print "Couldn't open file ";arg$(2)
  214.         parsecommandline=0%
  215.       end if
  216.     else
  217.       print "Couldn't open file ";arg$(1)
  218.       parsecommandline=0%
  219.     end if
  220.   end if
  221. end sub
  222.